package com.maven.booksystem.service;

import com.maven.booksystem.enums.BookStatusEnum;
import com.maven.booksystem.mapper.BookMapper;
import com.maven.booksystem.model.BookInfo;
import com.maven.booksystem.model.PageInfo;
import com.maven.booksystem.model.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;



@Service
public class BookService {

    @Autowired
    private BookMapper bookMapper;


    public List<BookInfo> getAllBook() {
        return bookMapper.getBookInfo();
    }

    public Integer insertBook(BookInfo bookInfo) {
        return bookMapper.insertBook(bookInfo);
    }

    // 获取翻页信息的 书籍
    public PageResult getBookByPage(PageInfo pageInfo) {
        PageResult<BookInfo> pageResult=new PageResult<>(pageInfo,bookMapper.queryListByPage(pageInfo),bookMapper.count());


        // 这里是 根据 枚举的 status状态类 来 设置相应书籍 的状态
        for(BookInfo bookInfo : pageResult.getRecords()) {
            bookInfo.setStateCN(BookStatusEnum.getStaticByCode(bookInfo.getStatus()).getDesc());
        }

        return pageResult;
    }

    // 根据 bookid 来获取 书籍
    public BookInfo getBookById(int id) {

        return bookMapper.getBookInfoById(id);
    }

    // 修改图书
    public Integer updateBook(BookInfo bookInfo) {
        return bookMapper.updateBook(bookInfo);
    }

    public Integer batchDelete(List ids) {
        return bookMapper.batchDelete(ids);
    }
}
